package com.x.sys.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.x.common.bo.PageObject;
import com.x.common.exception.ServiceException;
import com.x.sys.dao.SysRoleDao;
import com.x.sys.entity.SysRole;
import com.x.sys.service.SysRoleService;

@Service
public class SysRoleServiceImpl implements SysRoleService {
@Autowired
	private SysRoleDao sysRoleDao;

	@Override
	public PageObject<SysRole> findPageObjects(String name, Integer pageCurrent) {
		//参数校验
		
		if(pageCurrent==null||pageCurrent<1)
		throw new IllegalArgumentException("页码值无效");
		//查询总记录数,并进行校验
		int rowCount=sysRoleDao.getRowCount(name);
		if(rowCount==0)
		throw new ServiceException("没有对应的记录");
		//查询当前页记录
		int pageSize=4;
		//计算当前页查询的起始位置
		int startIndex=(pageCurrent-1)*pageSize;
		List<SysRole> records=
				sysRoleDao.findPageObjects(name, startIndex, pageSize);
		//对业务层查询结果进行处理和封装
		return new PageObject<>(records, rowCount, pageSize, pageCurrent);
	}

	@Override
	public int deleteObjects(Integer... ids) {
		//1.参数校验
		if(ids==null||ids.length==0)
			throw new IllegalArgumentException("参数值无效");
		//2.执行删除操作
		int rows=sysRoleDao.deleteObjects(ids);
		//3.验证删除结果
		if (rows==0)
			throw new ServiceException("记录可能不存在了");
		return rows;
	}
	
	
}
